<?php

namespace Admin\Controller;

use Think\Controller;
use Think\Exception;
use Think\Image;
use Think\Upload;
/*
create table `user`
(
    id          int auto_increment primary key,
    account     char(20)                not null default '' comment '用户账号',
    password    varchar(255)            not null default '' comment '登陆密码',
    nickname    char(19)                not null default '默认昵称' comment '用户昵称',
    mobile      char(11)                not null default 0 comment '手机号',
    mini_openid varchar(255)            not null default '0' comment '小程序openid',
    wx_openid   varchar(255)            not null default '0' comment '微信openid',
    head_img    varchar(255)            not null default '0' comment '用户头像',
    gender      enum ('0','1','2')      not null default '2' comment '性别,0:女,1:男,2:未知',
    role_id     tinyint unsigned        not null default 0 comment '用户角色ID',
    integral    int unsigned            not null default 0 comment '积分',
    money       decimal(12, 2) unsigned not null default 0 comment '余额',
    status      enum ('0','1')          not null default '1' comment '状态',
    create_time datetime comment '创建时间',
    update_time timestamp comment '修改时间'
) engine innodb comment '用户表';
 * */

class UserController extends Controller
{
    protected $arr = array('code' => 0, 'msg' => '', 'data' => array());

    public function login()
    {
        $account = I('post.account');
        $password = I('post.password');
//      $verify_code = I('post.verify_code');
        $user_model = D('user');
        $error = (int)session('error');
        try {
            $user_info = $user_model
                ->where(array('account' => $account))
                ->find();
            if (empty($user_info) || $user_info['role_id'] !== 1) {
                throw new Exception('非管理员账号', 1);
            }
            if (!password_verify($password, $user_info['password'])) {
                session('error', $error + 1);
                throw new Exception('密码有误', 2);
            }
            if ($user_info['status'] == 0) {
                throw new Exception('账号被禁用', 3);
            }
            unset($user_info['password']);
            session('uid', $user_info['id']);
            $this->arr['msg'] = '登陆成功!';
            $this->arr['code'] = 1;
            $this->arr['data'] = $user_info;
        } catch (Exception $e) {
            $this->arr['msg'] = $e->getMessage();
        }
    }

    /*列表
 *
 * */
    public function index()
    {
        try {
            /*----------------------------------------凑查询条件-------------------------------------------------------*/
            $where = array();


            $where = array_filter($where);
            /*---------------------------------------开始查询----------------------------------------------------------*/
            $model = D('user');
            $data = $model
                ->where($where)
                ->select();
            if (!is_array($data)) {
                throw new Exception('查询失败!' . $model->getError(), 1);
            }
            $this->arr['msg'] = I('msg') ? I('msg') : '查询成功';
            $this->arr['code'] = 1;
            $this->arr['data'] = $data;
        } catch (Exception $e) {
            $this->arr['msg'] = $e->getMessage();
        }
        $this->ajaxReturn($this->arr);
    }

    public function upload()
    {
        $upload = new Upload();
        $info = $upload->upload();
        if(!$info){
            echo $upload->getError();
        }else{
            dump($info);
            $path = './Uploads/'.$info['file']['savepath'].$info['file']['savename'];
            dump(stat($path));
            $img = new Image();
            $img = $img->open($path)->thumb(1000,1000)->save(date('H-i-s').'.jpg');
        }
    }
}